import log from "./logger";
import axios from "axios";
import * as moment from "moment";

const ADODB = require('node-adodb');
const connection = ADODB.open('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\AccessControl\\ICCard3000.MDB;jet oledb:database password=168168;');
//query操作的是有返回量的，比如查询
//execute操作是没有返回量的
setInterval(() => {
    // select r.f_RecID,r.f_CardNO,c.f_ConsumerNO,c.f_ConsumerName,r.f_ReadDate, a.f_ReaderName, r.f_Status
    //    from  ((t_d_SwipeRecord r left join t_b_Consumer c on r.f_ConsumerID = c.f_ConsumerID)
    //    left join t_b_Reader a on a.f_ReaderID = r.f_ReaderID  )
    //    where r.f_IsUpdate = 0
    connection.query('select r.f_RecID,r.f_CardNO,c.f_ConsumerNO,c.f_ConsumerName,r.f_ReadDate, a.f_ReaderName, r.f_Status \n' +
        'from  ((t_d_SwipeRecord r left join t_b_Consumer c on r.f_ConsumerID = c.f_ConsumerID) \n' +
        'left join t_b_Reader a on a.f_ReaderID = r.f_ReaderID) \n' +
        'where r.f_IsUpdate = 0').then((data: any) => {
        data.forEach((item: any) => {
            log.info(item);
            data = {
                status: item.f_Status === 0,
                doorTime: moment(item.f_ReadDate).format("YYYY-MM-DD hh:mm:ss"),
                department: "",
                platformId: 13,
                place: item.f_ReaderName,
                externalId: parseInt(item.f_RecID),
                name: item.f_ConsumerName,
                cardNum: item.f_CardNO,
                remarks: "",
                jobNum: item.f_ConsumerNO
            }
            log.info(data);
            axios.post('https://py.wechain360.com/api/weigh/internal/through/user', data).then(() => {
                connection.query(`update t_d_SwipeRecord set f_IsUpdate = 1 where f_RecID = ${item.f_RecID}`).then(()=>{
                    log.info('updated:{}', item.f_RecID)
                }).catch(()=>{})
            }).catch((e)=>{
                log.error(e);
            })
        });
    }).catch((err: any) => {
        log.error(err);
    })
}, 10000);
